﻿using System;

namespace Shell.DomainDot
{
    public abstract class DomainDotBase : IDisposable
    {
        private bool disposed;

        public DomainDotBase()
        {
            this.disposed = false;
            this.IsInitialized = false;
        }

        public DomainDotContext Context { get; internal set; }

        public virtual bool IsInitialized { get; private set; }

        public void Initialize()
        {
            this.OnInitialize();
            this.IsInitialized = true;
        }
        
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void OnInitialize()
        {
        }

        protected virtual void OnDispose(bool dispsing)
        {
        }

        private void Dispose(bool dispsing)
        {
            if (dispsing)
            {
                if (!this.disposed)
                {
                    this.OnDispose(dispsing);
                    this.Context = null;
                    this.IsInitialized = false;
                }
                this.disposed = true;
            }
        }
    }
}
